// pages/webView/webView.js
import p from "../../utils/platform";
import util from "../../utils/util.js";

const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    webViewUrl: "",
    optionsList: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true,
    });
    console.log("webview页面参数==", options);

    // 必须登录
    if (options.login == "1") {
      app
        .mobileLogin()
        .then((mobile) => {
          options.mobile = mobile;
          options.token = wx.getStorageSync("token");
          this.nextRender(options);
        })
        .catch(() => {
          app.globalData.afterMobileAuthorCallBack = () => {
            const { webViewUrl, activityName, activityCode, shareImgUrl } = options
            const url = `/pages/webView/webView?webViewUrl=${webViewUrl}&login=1&activityName=${activityName}&activityCode=${activityCode}&shareImgUrl=${shareImgUrl}`;
            
            wx.reLaunch({ url });
          };
        });
    } else {
      this.nextRender(options);
    }
  },

  nextRender(options) {
    let value = Object.keys(options)
      .filter((key) => {
        return key != "webViewUrl" && key != "shareImgUrl";
      })
      .map((v) => `${v}=${options[v]}`)
      .join("&");

    let url = options.webViewUrl + "?" + value;
    console.log("跳转地址：", url);

    this.setData({
      webViewUrl: url,
      optionsList: options,
    });
  },

  handleMessage(e) {
    console.log("从webview页面postmessage传过来", e.detail);
    const { data } = e.detail;
    if (data && data[0] && data[0].isGoZhaofang) {
      const queryStr = Object.keys(this.data.optionsList)
        .map((v) => `${v}=${this.data.optionsList[v]}`)
        .join("&");

      // console.log("跳找房小程序了====");

      const extraData = {
        vrHouseInfo: app.globalData.vrHouseInfo,
        type: "vr",
      };

      wx.navigateToMiniProgram({
        appId: "wx5adc5dd9870f6a26", //树懒找房极速版appid
        path: `/pages/webView/webView?${queryStr}`,
        extraData: extraData,
        envVersion: util.envVersion,
        success(res) {
          // 打开成功
        },
        fail(res) {
          //
        },
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const { shareImgUrl, activityName, activityCode } = this.data.optionsList;
    let userId = ''
    try {
      userId = getApp().globalData.sessionUser.id
    } catch (error) {}
    return {
      imageUrl: shareImgUrl || getApp().globalData.defaultShareImgUrl,
      title: activityName,
      path: `/pages/index/index?bannerCode=${activityCode}&agentId=${userId}`,
    };
  },
});
